﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using ExtremeML.Packaging;
using ExtremeML.Spreadsheet.Address;

namespace ExtremeML.Samples
{
	public static class WorksheetManipulation
	{
		/// <summary>
		/// Adds a new named worksheet to the workbook.
		/// </summary>
		/// <remarks>
		/// This method demonstrates adding a new worksheet to an existing workbook. An
		/// exception will be thrown if the specified must is not unique among its peer group.
		/// </remarks>
		public static void AddNew()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				package.WorkbookPart.WorksheetParts.Add("New Sheet");
			}
		}

		/// <summary>
		/// Inserts two new columns within an existing populated workbook.
		/// </summary>
		/// <remarks>
		/// This method demonstrates worksheet column insertion. Any supported worksheet
		/// elements to the right of the insertion point will be shifted horizontally.
		/// Refer to the user documentation for a list of OpenXml element types that
		/// are fully supported within ExtremeML for row and column insertion and deletion.
		/// </remarks>
		public static void InsertColumns()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Populated"];
				part.Worksheet.InsertColumns(2, 2);
			}
		}

		/// <summary>
		/// Inserts two new rows within an existing populated workbook.
		/// </summary>
		/// <remarks>
		/// This method demonstrates worksheet row insertion. Any supported worksheet
		/// elements below the insertion point will be shifted vertically.
		/// Refer to the user documentation for a list of OpenXml element types that
		/// are fully supported within ExtremeML for row and column insertion and deletion.
		/// </remarks>
		public static void InsertRows()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Populated"];
				part.Worksheet.InsertRows(5, 2);
			}
		}

		/// <summary>
		/// Deletes two columns within an existing populated workbook.
		/// </summary>
		/// <remarks>
		/// This method demonstrates worksheet column deletion. Any supported worksheet
		/// elements to the right of the deletion band will be shifted horizontally.
		/// Refer to the user documentation for a list of OpenXml element types that
		/// are fully supported within ExtremeML for row and column insertion and deletion.
		/// </remarks>
		public static void DeleteColumns()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Populated"];
				part.Worksheet.DeleteColumns(2, 2);
			}
		}

		/// <summary>
		/// Deletes two rows within an existing populated workbook.
		/// </summary>
		/// <remarks>
		/// This method demonstrates worksheet row deletion. Any supported worksheet
		/// elements below the deletion band will be shifted vertocally.
		/// Refer to the user documentation for a list of OpenXml element types that
		/// are fully supported within ExtremeML for row and column insertion and deletion.
		/// </remarks>
		public static void DeleteRows()
		{
			Utility.CreateFileFromResource(Utility.TemplateWorkbookResource, Utility.TargetOutputFile);

			using (var package = SpreadsheetDocumentWrapper.Open(Utility.TargetOutputFile))
			{
				var part = package.WorkbookPart.WorksheetParts["Populated"];
				part.Worksheet.DeleteRows(5, 2);
			}
		}
	}
}
